home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!friend!news
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Re: HELP! Modifying the EOF in a file!
- Message-ID: <1996Mar14.122030.9220@friend.kastle.com>
- Sender: news@friend.kastle.com (News)
- Reply-To: rich@kastle.com
- Organization: Kastle Development Associates
- X-Newsreader: Forte Free Agent 1.0.82
- References: <4i585k$4ia@news.netam.net> <4i5pb4$fae@sparcserver.lrz-muenchen.de>
- Date: Thu, 14 Mar 1996 12:19:52 GMT
-
- ua302aa@lrz-muenchen.de () wrote:
-
- >bgc@alpha.netam.net (The Bowling Green Connection) writes:
-
- >>I am having trouble shortening the size of a text file...
- >>Suppose I have a text file with 3 lines of data, then I
- >>run this program:
-
- >>void main() {
-
- >Next to no comment :-)
-
- Okay, I'll say it: No one should declare void main() and fail to
- return a valid result code to the execution environment, no matter how
- successfully the program compiles or runs.
-
- >> FILE *dat;
- >> dat=fopen("file.txt", "r+");
-
- >I fail to see where you want to _write_ to "file.txt"
-
- Look up "r+" mode and see where it says the file is opened for both
- reading and writing.
-
- >> fprintf(dat, "Hello! %c", EOF);
-
- >EOF cannot be a char, so printing it as a char might be a
- >bad idea.
-
- True.
-
- The C standard I/O library doesn't support this kind of thing. You
- can't just write EOF into a file like that. EOF is given not to be
- ANY possible character value, and the format specifier %c can't write
- anything other than a character value. Even if you used putc instead
- (which takes an int which could conceivably represent the value of EOF
- distinct from any char) it still won't work because the standard
- didn't say it could. In some file systems it's just not possible to
- take an existing file and change it's length, and so to be portable to
- those platforms, the Standard adopted this limitation.
-
- Therefore, according to the Standard, it's just not possible to do
- this. And if you believe that the only C programs are Standard C
- programs, you'd better stop reading.
-
- In MSDOS (and OS/2 and Windows and Windows NT), this works if you
- write the character value 26 (decimal, or '\032') instead of EOF.
- MSDOS text files' format harken back to the days of yore, of version
- 1.0, in the legacy of RT-11 and CP/M, when the file system couldn't
- tell the length of a file any more accurately that the number of
- sectors that contained it. Therefore CP/M put a character value in
- the data stream to show EOF for text files, so they wouldn't always
- have to have up to 511 extra characters at the end, and that character
- value is 26, Control-Z. Nowadays the MSDOS file system knows how long
- files are to the byte and the 26 isn't needed, but for compatability
- it is still interpreted that way.
-
- (RT-11's solution is a bit different. RT-11 text files are
- interpreted as not containing any 0 characters, they may be in fact be
- interspersed in the text. Typically the last sector of an RT-11 text
- file is filled with 0s.)
-
- Note however that if you do this the file doesn't really get shorter.
- It takes the same amount of disk space, and programs that read the
- file in binary ("rb") can read your '\032', and right past into the
- remaining data, until the real EOF.
-
- Also note that this will not work with unix or unix-like systems. 26
- is just a character value with no special significance. Most provide
- some other special IO function to shorten a file. It might be called
- chsize().
-
- --
- Richard Krehbiel, Kastle Systems, Arlington VA USA
- rich@kastle.com (work) or richk@mnsinc.com (personal)
-
-